//
//  WOSettingsViewController.m
//  WOPrototype
//
//  Created by Alexander Skogberg on 2010-04-27.
//  Copyright 2010 Chalmers tekniska högskola. All rights reserved.
//

#import "WOPrototypeAppDelegate.h"
#import "WOSettingsViewController.h"

@implementation WOSettingsViewController

@synthesize changeUserButton;
@synthesize loadContactsButton;
@synthesize loadContactsLabel;

#pragma mark Initialization Methods

/* View is created */
- (void)viewDidLoad 
{
    [super viewDidLoad];
	
	//Set navigation bar title.
	UILabel *titleView = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 30.0f)] autorelease];
	[titleView setBackgroundColor:[UIColor clearColor]];
	[titleView setTextColor:[UIColor whiteColor]];
	[titleView setTextAlignment:UITextAlignmentCenter];
	[titleView setText:@"Inställningar"];
	self.navigationItem.titleView = titleView;
	
	//Set background.
	self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
	self.title = @"Inställningar";
	
	//Show current user.
	WOPrototypeAppDelegate *appDelegate = (WOPrototypeAppDelegate *)[[UIApplication sharedApplication] delegate];
	NSString *userName = [NSString stringWithFormat:@"%@ %@", appDelegate.currentUser.firstName, appDelegate.currentUser.lastName];
	[self.changeUserButton setTitle:userName forState:UIControlStateNormal];
}

/* The view will be pushed */
- (void) viewWillAppear:(BOOL)animated
{
	//Get the latest contact load date.
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	NSString *loadContactsInfo = [defaults objectForKey:@"latestLoad"];
	
	if (loadContactsInfo != nil)
		[self.loadContactsLabel setText:loadContactsInfo];
	else
		[self.loadContactsLabel setText:@""];
}

/* Handles iPhone screen tilting */
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
	return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark -

#pragma mark Others Methods

/* The button for changing user is tapped */
- (IBAction) changeUser
{
	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Välj användare"
															 delegate:self
													cancelButtonTitle:nil
											   destructiveButtonTitle:nil
													otherButtonTitles:nil];
	
	WOPrototypeAppDelegate *appDelegate = (WOPrototypeAppDelegate *)[[UIApplication sharedApplication] delegate];
	
	for (WOPerson *user in appDelegate.users)
		[actionSheet addButtonWithTitle:[NSString stringWithFormat:@"%@ %@", user.firstName, user.lastName]];
	
	[actionSheet addButtonWithTitle:@"Tillbaka"];
	[actionSheet setCancelButtonIndex:appDelegate.users.count];

	[actionSheet showInView: self.view.window];
}

- (IBAction) loadContacts
{
	WOPrototypeAppDelegate *appDelegate = (WOPrototypeAppDelegate *)[[UIApplication sharedApplication] delegate];
	[appDelegate addPeopleToAddressBook:appDelegate.users];
	
	//Set load date info.
	NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
	[myDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
	NSString *currentDate = [myDateFormatter stringFromDate:[NSDate date]];
	[myDateFormatter release];
	
	NSString *loadContactsInfo = [NSString stringWithFormat:@"Kontakter senast hämtade: %@", currentDate];
	[self.loadContactsLabel setText:loadContactsInfo];

	//Save load date info.
	[[NSUserDefaults standardUserDefaults] setObject:loadContactsInfo forKey:@"latestLoad"];
}

#pragma mark UIActionSheet Delegate Methods

/* A button in the UIActionSheet for selecting users is tapped */
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{	
	WOPrototypeAppDelegate *appDelegate = (WOPrototypeAppDelegate *)[[UIApplication sharedApplication] delegate];
	
	if (buttonIndex != actionSheet.cancelButtonIndex)
	{
		appDelegate.currentUser = [appDelegate.users objectAtIndex:buttonIndex];
	}
	[actionSheet release];
	
	//Set button title.
	NSString *userName = [NSString stringWithFormat:@"%@ %@", appDelegate.currentUser.firstName, appDelegate.currentUser.lastName];
	[self.changeUserButton setTitle:userName forState:UIControlStateNormal];
}

#pragma mark -

@end